package com.gugee.tiktok.web.service;


import com.gugee.common.web.system.user.model.VerityCode;
import com.gugee.common.web.system.user.repository.VerityCodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class VerityCodeService {
    @Autowired
    VerityCodeRepository verityCodeRepository;

    @Value("${verity.code.expired}")
    Long expired;

    public void save(VerityCode verityCode){
        verityCodeRepository.save(verityCode);
    }

    public Boolean validate(String email,String code){
        VerityCode verityCode = verityCodeRepository.findFirstByEmailAndCode(email,code);

        if(null != verityCode){
            Long localTime = new Date().getTime()/1000;

            if(verityCode.getCreateTime() + expired - localTime  >= 0 ){
                verityCodeRepository.deleteById(verityCode.getId());
                return true;
            }
        }
        return false;
    }
}
